﻿using Newtonsoft.Json;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace ManhattanProcedure.Model
{
    public class SocketClients
    {
        /// <summary>
        /// 左边
        /// </summary>
        public int ZBBS = 0;
        /// <summary>
        /// 右边
        /// </summary>
        public int YBBS = 0;
        private static SocketClients instance;
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;

        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse(IPorPortInfo.Instance.CoffeeSocketIP);
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 4;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        if (ClientList.code == 0)
                        {
                            show(ClientList.message.type, ClientList.message.Name);
                        }
                        else if (ClientList.code == 3)
                        {
                            RobotShow(ClientList.message.Name, ClientList.message.type);
                            ClientList clientList = new ClientList();
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.robotMsg = RobotMsg.Instance;
                            clientList.code = 3;
                            clientList.IsMake = MainWindow.isMake;
                            var list = JsonConvert.SerializeObject(clientList);
                            var msgs = Encoding.UTF8.GetBytes(list);
                            client.Send(msgs);
                            RobotOperate.CX_Start_L();
                            RobotOperate.CX_Start_R();
                            Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                            if (RobotOperate.pstatusL == JKTYPE.ProgramState.PROGRAM_RUNNING && RobotOperate.pstatusR == JKTYPE.ProgramState.PROGRAM_RUNNING)
                            {
                                if (WuLiaoCoffeeSel.Instance.RobotWarn_Sel_Bol != true)
                                {

                                    WuLiaoCoffeeSel.Instance.RobotWarn_Sel_Bol = true;

                                }
                            }
                            else
                            {
                                if (WuLiaoCoffeeSel.Instance.RobotWarn_Sel_Bol != false)
                                {
                                    WuLiaoCoffeeSel.Instance.RobotWarn_Sel_Bol = false;

                                }
                            }
                            ClientList clientList2 = new ClientList();
                            clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList2.code = 2;
                            client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                        }
                        else if (ClientList.code == 6)
                        {
                            MainWindow.MakingTypes = ClientList.message.type;
                            MainWindow.img_path = ClientList.message.Name;
                        }
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }
        public void show(Double code, string msg)
        {
            try
            {
                CoffeeIOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                switch (msg)
                {
                    case "LB8":
                        if (code == 0)
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.Drop_Cup_start);
                            Thread.Sleep(200);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.Drop_Cup_end);
                            Thread.Sleep(200);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "TGS_BTN":
                        if (code == 0)
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);

                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_start);

                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "TGX_BTN":
                        if (code == 0)
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_start);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "CCMSS":
                        if (code == 0)
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_start);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "CCMXJ":
                        if (code == 0)
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_start);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            CoffeeIOMsg.Instance.AllBol = true;
                        }
                        break;
                    default:
                        break;
                }
                Thread.Sleep(100);
                CoffeeIOMsg.Instance.AllBol = true;
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, double Type)
        {
            try
            {
                if (name == "Reload_L")
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle1);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle1);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle1);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle1);
                            break;
                        case 7:
                            jakaAPI.program_run(ref RobotOperate.handle1);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle1);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle1);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle1);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle1);
                            break;
                    }
                    RobotOperate.Instance.Robot_L_Show();
                }
                else if (name == "Reload_R")
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle2);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle2);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle2);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle2);
                            break;
                        case 7:
                            jakaAPI.program_run(ref RobotOperate.handle2);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle2);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle2);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle2);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle2);
                            break;
                    }
                    RobotOperate.Instance.Robot_R_Show();
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public static void QBMsg(int cupcode)
        {
            try
            {
                CoffeeIOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QB_Robot_Left_Start);
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    CoffeeIOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DLB_Robot_Left_Sel);
                    Thread.Sleep(200);
                    if (CoffeeIOMsg.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Left_End);
                        Thread.Sleep(200);
                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JBSB_Robot_Left_End);
                        Thread.Sleep(200);
                        break;
                    }
                }
                Thread.Sleep(200);
                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.Drop_Cup_start);
                Thread.Sleep(100);
                CoffeeIOMsg.Instance.AllBol = true;
                Thread.Sleep(100);
                while (WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                while (!WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                CoffeeIOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.Drop_Cup_end);
                Thread.Sleep(500);
                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.BJC_Robot_Left_Start);
                Thread.Sleep(500);
                CoffeeIOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(100);
                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DJBJC_Robot_Left_Sel);
                    Thread.Sleep(100);
                    if (CoffeeIOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                }
                CoffeeIOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        //裁剪图像
        public Bitmap CropImage(Bitmap originImage, Rectangle region)
        {
            Bitmap result = new Bitmap(region.Width, region.Height);
            Graphics graphics = Graphics.FromImage(result);
            graphics.DrawImage(originImage, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel);
            return result;
        }
        public Bitmap DrawPic(Bitmap b, float angle)
        {
            //调用旋转函数
            Bitmap bb = RotateImg2(b, angle);
            int W = b.Width; //最大值
            int H = b.Height;//最大值
                             //旋转后图片大小
            int w = bb.Width;
            int h = bb.Height;
            //W×H图像大小背景图像坐标系到图像本身坐标系(旋转后图像）
            int move_x = (W - w) / 2;
            int move_y = (H - h) / 2;
            Bitmap aa = new Bitmap(W, H);//建立背景图片，在此基础上绘制
            for (int ii = 0; ii < W; ii++)
            {
                for (int jj = 0; jj < H; jj++)
                {
                    aa.SetPixel(ii, jj, System.Drawing.Color.FromArgb(255, 255, 255));
                }
            };
            Graphics g01 = Graphics.FromImage(aa);
            g01.TranslateTransform(move_x, move_y);//坐标系平移
            g01.DrawImage(bb, new PointF(0, 0));//坐标系平移后绘制图片

            int x = (W - w) / 2;
            int y = (H - h) / 2;
            //裁剪图片外围空白区域
            Rectangle cropRegion = new Rectangle(0, 0, W, H);
            Bitmap cc = CropImage(aa, cropRegion); //最终的图像数据
            return cc;
        }


        public Bitmap RotateImg2(Bitmap b, float angle)
        {
            angle = angle % 360;            //弧度转换
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);
            //原图的宽和高
            int w = b.Width;
            int h = b.Height;
            //计算旋转后图片的大小
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
            //目标位图，也是在该图片上进行绘制
            Bitmap dsImage = new Bitmap(W, H);
            Graphics g = Graphics.FromImage(dsImage);
            g.InterpolationMode = InterpolationMode.Bilinear;
            g.SmoothingMode = SmoothingMode.HighQuality;
            //计算偏移量
            System.Drawing.Point Offset = new System.Drawing.Point((W - w) / 2, (H - h) / 2);
            //构造图像显示区域：让图像的中心与窗口的中心点一致
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            System.Drawing.Point center = new System.Drawing.Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            //参考系原点从图像左上角平移到中心，以实现图片绕中心旋转
            g.TranslateTransform(center.X, center.Y);
            //旋转参考系，相当于旋转图片
            g.RotateTransform(360 - angle);
            //恢复图像在水平和垂直方向的平移，坐标系原点恢复至旋转后坐标系的左上角
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);
            //重至绘图的所有变换
            g.ResetTransform();
            g.Save();
            g.Dispose();
            //dsImage.Save("yuancd.png", System.Drawing.Imaging.ImageFormat.Png);
            return dsImage;
        }
        /// <summary>
        /// 获取网络图片转换为base64
        /// </summary>
        /// <param name="url">网络图片路径   例：http://123.123.123.123/a.jpg</param>
        /// <returns></returns>
        public string GetUrlToBase64(string url)
        {
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                string Base64string = Convert.ToBase64String(Bytes);
                // return "data:image/jpeg;base64," + base64String;
                return base64String;
            }
            catch (Exception e)
            {
                return "";
            }
        }
        public Bitmap rotateImage(Bitmap b, float angle)
        {
            try
            {
                //创建位图用于旋转图片
                Double tangle = angle * Math.PI / 180;
                Console.WriteLine(tangle);
                int r = (int)Math.Sqrt((b.Width * b.Width) + (b.Height * b.Height));
                int w = (int)(Math.Abs(b.Height * Math.Sin(tangle)) + Math.Abs(b.Width * Math.Cos(tangle)));
                int h = (int)(Math.Abs(b.Height * Math.Cos(tangle)) + Math.Abs(b.Width * Math.Sin(tangle)));
                Console.WriteLine(w);
                Console.WriteLine(h);
                Console.WriteLine(b.Width);
                Console.WriteLine(b.Height);
                Bitmap returnBitmap = new Bitmap(w, h);

                //初始化Graphics类
                Graphics g = Graphics.FromImage(returnBitmap);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                //移动坐标原点到图片中心
                g.TranslateTransform((float)w / 2, (float)h / 2);
                //顺时针旋转对应angle的角度
                g.RotateTransform(angle);
                //移动坐标原地到原来的位置
                g.TranslateTransform(-(float)w / 2, -(float)h / 2);
                g.Clear(System.Drawing.Color.White);
                //构造图片
                g.DrawImage(b, new System.Drawing.Point((w - b.Width) / 2, (h - b.Height) / 2));

                g.Dispose();
                //返回位图
                return returnBitmap;
            }
            catch (Exception)
            {

                return null;
            }

        }
        public string ToBase64(Bitmap bmp)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                String strbaser64 = Convert.ToBase64String(arr);
                return "data:image/jpeg;base64," + strbaser64;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public Bitmap Base64StringToImage(string inputStr)
        {
            try
            {
                byte[] arr = Convert.FromBase64String(inputStr);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                ms.Close();
                return bmp;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }
        /// <summary>
        /// 获取网络图片转换为base64
        /// </summary>
        /// <param name="url">网络图片路径   例：http://123.123.123.123/a.jpg</param>
        /// <returns></returns>
        //public string GetUrlToBase64(string url)
        //{
        //    try
        //    {
        //        var base64String = "";
        //        HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
        //        HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
        //        string base64sstr = string.Empty;
        //        using (Stream stream = webResponse.GetResponseStream())
        //        {
        //            var mStream = new MemoryStream();

        //            byte[] buffer = new byte[1024];
        //            int byteCount;
        //            do
        //            {
        //                byteCount = stream.Read(buffer, 0, buffer.Length);
        //                mStream.Write(buffer, 0, byteCount);
        //            } while (byteCount > 0);

        //            mStream.Seek(0, SeekOrigin.Begin);
        //            base64String = Convert.ToBase64String(mStream.ToArray());
        //        }
        //        //return base64String;
        //        WebClient mywebclient = new WebClient();
        //        byte[] Bytes = mywebclient.DownloadData(url);
        //        string Base64string = Convert.ToBase64String(Bytes);
        //        return "data:image/jpeg;base64," + base64String;
        //    }
        //    catch (Exception e)
        //    {
        //        return "";
        //    }
        //}
        public void ClientR()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (MainWindow.IsStartMakingR)
                        {
                            MainWindow.IsStartMakingR = false;
                            int code = MainWindow.coffeeNameCode;
                            int codeMid = code / 100;
                            int codeMP = code % 100;
                            if (codeMid == 52)//加冰品
                            {
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.BKF_Robot_Right_Start);
                                Thread.Sleep(300);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Right_Start);
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(300);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DZBJ_Robot_Right_Sel);
                                    Thread.Sleep(300);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(200);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Right_End);
                                Thread.Sleep(200);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.BKF_Robot_Right_End);
                                IceMakingMsg.Instance.AllBol = false;
                                if (MainWindow.coffeeNameCode == 5212)
                                {
                                    Thread.Sleep(100);
                                    IceMakingSerialPort.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                    Thread.Sleep(100);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QJN_Robot_Right_Start);
                                }
                                else
                                {
                                    Thread.Sleep(100);
                                    IceMakingSerialPort.Instance.SerialSend(IceMakingModel.Instance.IceWaterMaking);
                                    Thread.Sleep(100);
                                }
                                IceMakingMsg.Instance.AllBol = true;
                                Thread.Sleep(5000);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.CBWC_Robot_Right_Start);
                                Thread.Sleep(100);
                                if (MainWindow.coffeeNameCode == 5212)
                                {
                                    CoffeeIOMsgR.Instance.RobotBol = false;
                                    while (true)
                                    {
                                        Thread.Sleep(300);
                                        CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DKFJ_Robot_Right_Sel);
                                        Thread.Sleep(300);
                                        if (CoffeeIOMsgR.Instance.RobotBol)
                                        {
                                            break;
                                        }
                                    }
                                    #region 咖博士
                                    coffeeMsg.Instance.AllBol = false;
                                    Thread.Sleep(500);
                                    MakeCoffeeNow(code.ToString());
                                    Thread.Sleep(500);
                                    coffeeMsg.Instance.AllBol = true;
                                    #endregion
                                    #region eversys通讯
                                    //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                    //Thread.Sleep(200);
                                    //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, 1);
                                    //Thread.Sleep(200);
                                    //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                    #endregion
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QJN_Robot_Right_End);
                                    Thread.Sleep(5000);
                                    while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                    Thread.Sleep(1000);
                                    while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                    //  while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                    Thread.Sleep(2000);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.JNWC_Robot_Right_Start);
                                    Thread.Sleep(100);
                                }
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(300);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DDBD_Robot_Right_Sel);
                                    Thread.Sleep(300);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }

                                while (!CoffeeMakeModel.Instance.IsDJBL) { Thread.Sleep(10); }
                                Thread.Sleep(100);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.JNWC_Robot_Right_End);
                                Thread.Sleep(100);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QDB_Robot_Right_Start);
                                Thread.Sleep(300);
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(300);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DBWC_Robot_Right_Sel);
                                    Thread.Sleep(300);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                CoffeeMakeModel.Instance.IsDBWCL = true;
                                YBBS = 1;
                            }
                            else if (codeMid == 53)//拉花品
                            {
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.LHKF_Robot_Right_Start);
                                Thread.Sleep(300);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Right_Start);
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                //CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.LRobot_KFJR_UP);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DKFJ_Robot_Right_Sel);
                                    Thread.Sleep(100);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(200);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Right_End);
                                while (!CoffeeMakeModel.Instance.IsDKFJL) { Thread.Sleep(100); }
                                #region 咖博士
                                coffeeMsg.Instance.AllBol = false;
                                Thread.Sleep(500);
                                MakeCoffeeNow(code.ToString());
                                Thread.Sleep(500);
                                coffeeMsg.Instance.AllBol = true;
                                #endregion
                                #region eversys通讯
                                //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                //Thread.Sleep(200);
                                //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, 1);
                                //Thread.Sleep(200);
                                //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                #endregion
                                Thread.Sleep(5000);
                                while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                Thread.Sleep(1000);
                                while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                //  while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                Thread.Sleep(2000);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.JNWC_Robot_Right_Start);
                                Thread.Sleep(100);
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.DLHD_Robot_Right_Sel);
                                    Thread.Sleep(200);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                CoffeeMakeModel.Instance.IsDLHDR = true;
                                YBBS = 1;
                                while (!CoffeeMakeModel.Instance.IsLHWCR) { Thread.Sleep(10); }
                                while (true)
                                {
                                    RobotOperate.CX_Start_R();
                                    if (RobotOperate.pstatusR == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(100);
                                }
                                Thread.Sleep(100);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QQX_Robot_Right_Start);
                               // Console.WriteLine(CoffeeIOModel.Instance.LRobot_XBR_UP);
                                Thread.Sleep(100);
                                CoffeeIOMsgR.Instance.RobotBol = false;
                                while (true)
                                {
                                    Thread.Sleep(500);
                                    CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QXWC_Robot_Right_Sel);
                                    Thread.Sleep(500);
                                    if (CoffeeIOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(500);
                                CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.QQX_Robot_Right_End);
                                Thread.Sleep(500);
                              //  Console.WriteLine(CoffeeIOModel.Instance.LRobot_XBR_Down);
                                CoffeeMakeModel.Instance.IsDXBDR = true;
                            }
                            else
                            {
                                YBBS = 1;
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Console.WriteLine("右臂");
                    Thread.Sleep(1000);
                }

            }
            catch (Exception)
            {
                Console.WriteLine("youbi");
            }
        }
        public int CoffeeSel(int codeMP)
        {
            try
            {
                int code = 0;
                switch (codeMP)
                {
                    case 3://卡布奇诺
                        if (MainWindow.cupCode == 2)
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 3;
                            else
                                code = 16;
                        }
                        else
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 10;
                            else
                                code = 20;
                        }
                        break;
                    case 4://拿铁
                        if (MainWindow.cupCode == 2)
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 4;
                            else
                                code = 17;
                        }
                        else
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 11;
                            else
                                code = 21;
                        }
                        break;
                    case 1://意式
                        if (MainWindow.BeanCode == 1)
                            code = 1;
                        else
                            code = 14;
                        break;
                    case 2://美式
                        if (MainWindow.cupCode == 2)
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 2;
                            else
                                code = 15;
                        }
                        else
                        {
                            if (MainWindow.BeanCode == 1)
                                code = 9;
                            else
                                code = 19;
                        }
                        break;
                    case 8://冰美式
                        if (MainWindow.BeanCode == 1)
                            code = 8;
                        else
                            code = 18;
                        break;
                    case 12://冰拿铁
                        if (MainWindow.BeanCode == 1)
                            code = 12;
                        else
                            code = 22;
                        break;
                    default:
                        code = codeMP;
                        break;
                }
                return code;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 打印图片保存
        /// </summary>
        /// <param name="ImageData"></param>
        /// <param name="imageName"></param>
        private void SaveImage(byte[] ImageData, string imageName)
        {
            try
            {
                //保存图片到本地文件夹
                System.IO.MemoryStream ms = new System.IO.MemoryStream(ImageData);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                //保存到磁盘文件
                string imagePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印";
                if (!System.IO.Directory.Exists(imagePath))
                    System.IO.Directory.CreateDirectory(imagePath);
                img.Save(System.IO.Path.Combine(imagePath, imageName), System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Dispose();
            }
            catch (Exception exception)
            { };
        }
        public void MakeCoffeeNow(string coffeename)
        {
            try
            {
                switch (coffeename)
                {
                    case "5001":
                    case "5101":
                    case "5201":
                    case "5301":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5002":
                    case "5102":
                    case "5202":
                    case "5302":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.americano);
                        break;
                    case "5003":
                    case "5103":
                    case "5203":
                    case "5303":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.cappuccino);
                        break;
                    case "5004":
                    case "5104":
                    case "5204":
                    case "5304":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Espresso2);
                        break;
                    case "5005":
                    case "5105":
                    case "5205":
                    case "5305":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.latte);
                        break;
                    case "5006":
                    case "5106":
                    case "5206":
                    case "5306":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.hotmilk);
                        break;
                    case "5007":
                    case "5107":
                    case "5207":
                    case "5307":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.hotfoam);
                        break;
                    case "5008":
                    case "5108":
                    case "5208":
                    case "5308":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.water);
                        break;
                    case "5009":
                    case "5109":
                    case "5209":
                    case "5309":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.flatwhite);
                        break;
                    case "5010":
                    case "5110":
                    case "5210":
                    case "5310":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.macchiato);
                        break;
                    case "5011":
                    case "5111":
                    case "5211":
                    case "5311":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.lattemacchiato);
                        break;
                    case "5012":
                    case "5112":
                    case "5212":
                    case "5312":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.potcoffee);
                        break;
                    case "5013":
                    case "5113":
                    case "5213":
                    case "5313":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.cremacoffee);
                        break;
                    case "5014":
                    case "5114":
                    case "5214":
                    case "5314":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.AmericanIce);
                        break;
                    case "5015":
                    case "5115":
                    case "5215":
                    case "5315":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.ristretto);
                        break;
                    case "5016":
                    case "5116":
                    case "5216":
                    case "5316":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.americamilk);
                        break;
                    case "5017":
                    case "5117":
                    case "5217":
                    case "5317":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espressomilk);
                        break;
                    case "5018":
                    case "5118":
                    case "5218":
                    case "5318":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.bycoffee);
                        break;
                    case "5019":
                    case "5119":
                    case "5219":
                    case "5319":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.ristretto2);
                        break;
                    case "5020":
                    case "5120":
                    case "5220":
                    case "5320":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.cappuccino2);
                        break;
                    case "5021":
                    case "5121":
                    case "5221":
                    case "5321":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.latte2);
                        break;
                    case "5022":
                    case "5122":
                    case "5222":
                    case "5322":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.flatwhite2);
                        break;
                    case "5023":
                    case "5123":
                    case "5223":
                    case "5323":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.latteice);
                        break;
                    case "5024":
                    case "5124":
                    case "5224":
                    case "5324":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.ameracanobig);
                        break;
                    case "5025":
                    case "5125":
                    case "5225":
                    case "5325":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.milkcap);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void ClientL()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (MainWindow.IsStartMakingL)
                        {
                            MainWindow.IsStartMakingL = false;
                            int code = MainWindow.coffeeNameCode;
                            int codeMid = code / 100;
                            int codeMP = code % 100;
                            codeMP = CoffeeSel(codeMP);
                            CoffeeIOMsg.Instance.AllBol = false;
                            Thread.Sleep(500);
                            if (codeMid == 50)//直接饮品
                            {
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Left_Start);
                                Thread.Sleep(200);
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Left_Start);
                            }
                            else if (codeMid == 51)//打印品
                            {
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DYKF_Robot_Left_Start);
                                Thread.Sleep(200);
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DYKF_Robot_Left_Start);
                            }
                            else if (codeMid == 52)//加冰品
                            {
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.BKF_Robot_Left_Start);
                                Thread.Sleep(200);
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.BKF_Robot_Left_Start);
                            }
                            else if (codeMid == 53)//拉花品
                            {
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LHKF_Robot_Left_Start);
                                Thread.Sleep(200);
                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LHKF_Robot_Left_Start);
                            }
                            Thread.Sleep(500);
                            QBMsg(MainWindow.cupCode);
                            Thread.Sleep(200);
                            CoffeeIOMsg.Instance.AllBol = true;
                            Thread.Sleep(500);
                            while (true)
                            {
                                if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol)
                                {
                                    CoffeeIOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JBCG_Robot_Left_Start);
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QB_Robot_Left_End);

                                    if (MainWindow.sugarCode != 0)
                                    {
                                        Thread.Sleep(200);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT_Robot_Left_Start);
                                        Thread.Sleep(200);
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DGTD_Robot_Left_Sel);
                                            Thread.Sleep(100);
                                            if (CoffeeIOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        //if (MainWindow.sugarCode == 1)
                                        //{
                                        //    Thread.Sleep(100);
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT1_start);
                                        //    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.SugarInit1 * 1000));
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT1_end);
                                        //    Thread.Sleep(1000);
                                        //}
                                        //else if (MainWindow.sugarCode == 2)
                                        //{
                                        //    Thread.Sleep(100);
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT2_start);
                                        //    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.SugarInit2 * 1000));
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT2_end);
                                        //    Thread.Sleep(1000);
                                        //}
                                        //else if (MainWindow.sugarCode == 3)
                                        //{
                                        //    Thread.Sleep(100);
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT3_start);
                                        //    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.SugarInit3 * 1000));
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GT3_end);
                                        //    Thread.Sleep(1000);
                                        //}
                                        Thread.Sleep(200);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.GTWC_Robot_Left_Start);
                                        Thread.Sleep(200);
                                    }
                                    //50 接咖啡 51 打印 52 加冰 53 拉花
                                    if (codeMid == 50)
                                    {
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        CoffeeIOMsg.Instance.RobotBol = false;
                                        //while (true)
                                        //{
                                        //    Thread.Sleep(100);
                                        //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LRobot_DYD);
                                        //    Thread.Sleep(100);
                                        //    if (CoffeeIOMsg.Instance.RobotBol)
                                        //    {
                                        //        break;
                                        //    }
                                        //}
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QDY_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DYKF_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JKF_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        #region 咖博士
                                        coffeeMsg.Instance.AllBol = false;
                                        Thread.Sleep(500);
                                        MakeCoffeeNow(code.ToString());
                                        Thread.Sleep(500);
                                        coffeeMsg.Instance.AllBol = true;
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        Thread.Sleep(1000);
                                        while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        Thread.Sleep(1000);
                                        while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        #endregion
                                        #region eversys通讯
                                        //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, codeMP);
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        //Console.WriteLine("yes3");
                                        //Thread.Sleep(200);
                                        //CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LRobot_KFJ_Down);
                                        //Thread.Sleep(100);
                                        //CoffeeIOMsg.Instance.AllBol = true;
                                        //Thread.Sleep(5000);
                                        //while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        //Thread.Sleep(2000);
                                        //CoffeeIOMsg.Instance.AllBol = false;
                                        #endregion
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KFWC_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        Thread.Sleep(100);
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 51)
                                    {
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        CoffeeIOMsg.Instance.RobotBol = false;
                                        #region eversys通讯
                                        //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, codeMP);
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        #endregion
                                        #region 咖博士
                                        coffeeMsg.Instance.AllBol = false;
                                        Thread.Sleep(500);
                                        MakeCoffeeNow(code.ToString());
                                        Thread.Sleep(500);
                                        coffeeMsg.Instance.AllBol = true;
                                        #endregion
                                        //Thread.Sleep(3000);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QDY_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DYKF_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JKF_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        string imgname = "";
                                        try
                                        {
                                            var imgs = GetUrlToBase(MainWindow.Img);
                                            SaveImage(imgs, "打印图片.jpg");
                                            //base64url = GetUrlToBase64(img);
                                            //Console.WriteLine(imgname);
                                            //Console.WriteLine("打印 " + DateTime.Now.ToString());
                                            //var base64url = GetUrlToBase64(MainWindow.Img);
                                            //Console.WriteLine("打印1" + DateTime.Now.ToString());
                                            //if (base64url != "")
                                            //{
                                            //    var bmp = Base64StringToImage(base64url);
                                            //    if (bmp != null)
                                            //    {
                                            //        var bmp145 = DrawPic(bmp, 145);//rotateImage(bmp, -145);
                                            //        if (bmp145 != null)
                                            //        {
                                            //            imgname = ToBase64(bmp145);
                                            //            Console.WriteLine(imgname);
                                            //        }
                                            //    }
                                            //}
                                            // SaveImage(imgs, "打印图片.jpg");
                                        }
                                        catch (Exception)
                                        {
                                        }
                                        Thread.Sleep(5000);
                                        while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        Thread.Sleep(1000);
                                        while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        //while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        Thread.Sleep(5000);
                                        //Console.WriteLine(CoffeeErversysMsg.Instance.IsReadyMilkL);
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KFWC_Robot_Left_Start);
                                        Thread.Sleep(200);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QB_Robot_Left_End);
                                        Thread.Sleep(1000);
                                        CoffeeIOMsg.Instance.RobotBol = false;
                                        Thread.Sleep(1000);
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DDYJ_Robot_Left_Sel);
                                            Thread.Sleep(100);
                                            if (CoffeeIOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        #region 新的打印机
                                        PwebSockets.Instance.DyCS(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印\打印图片.jpg");
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DYWC_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        #endregion
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 52)
                                    {
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        CoffeeIOMsg.Instance.RobotBol = false;
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DJBD_Robot_Left_Sel);
                                            Thread.Sleep(100);
                                            if (CoffeeIOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        CoffeeMakeModel.Instance.IsDJBL = true;
                                        while (!CoffeeMakeModel.Instance.IsDBWCL) { Thread.Sleep(10); }
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QDY_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.BKF_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KSZZ_Robot_Left_End);
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JKF_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        #region 咖博士
                                        coffeeMsg.Instance.AllBol = false;
                                        Thread.Sleep(500);
                                        MakeCoffeeNow(code.ToString());
                                        Thread.Sleep(500);
                                        coffeeMsg.Instance.AllBol = true;
                                        #endregion
                                        #region eversys通讯
                                        //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, codeMP);
                                        //Thread.Sleep(200);
                                        //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        #endregion
                                        Console.WriteLine("yes3");
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        Thread.Sleep(5000);
                                        while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        Thread.Sleep(1000);
                                        while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                        //while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        Thread.Sleep(2000);
                                        CoffeeIOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KFWC_Robot_Left_Start);
                                        Thread.Sleep(100);
                                        CoffeeIOMsg.Instance.AllBol = true;
                                        Thread.Sleep(100);
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 53)
                                    {
                                        try
                                        {
                                            CoffeeIOMsg.Instance.AllBol = false;
                                            CoffeeIOMsg.Instance.RobotBol = false;
                                            //while (true)
                                            //{
                                            //    Thread.Sleep(100);
                                            //    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LRobot_DYD);
                                            //    Thread.Sleep(100);
                                            //    if (CoffeeIOMsg.Instance.RobotBol)
                                            //    {
                                            //        break;
                                            //    }
                                            //}
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QDY_Robot_Left_End);
                                            Thread.Sleep(200);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LHKF_Robot_Left_End);
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JKF_Robot_Left_Start);
                                            Thread.Sleep(100);
                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DKFJ_Robot_Left_Sel);
                                                Thread.Sleep(100);
                                                if (CoffeeIOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }

                                            Thread.Sleep(100);
                                            #region 咖博士
                                            coffeeMsg.Instance.AllBol = false;
                                            Thread.Sleep(500);
                                            MakeCoffeeNow("5001");
                                            Thread.Sleep(500);
                                            coffeeMsg.Instance.AllBol = true;
                                            //Thread.Sleep(100);
                                            //CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.LRobot_KFJ_Down);
                                            //Thread.Sleep(100);
                                            //CoffeeIOMsg.Instance.AllBol = true;
                                            //Thread.Sleep(1000);
                                            //while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                            //Thread.Sleep(1000);
                                            //while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                            #endregion
                                            #region eversys通讯
                                            //CoffeeErversysMsg.Instance.iscoffeeready = false;
                                            //Thread.Sleep(200);
                                            //CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, 1);
                                            //Thread.Sleep(200);
                                            //CoffeeErversysMsg.Instance.iscoffeeready = true;
                                            #endregion
                                            Console.WriteLine("yes3");
                                            Thread.Sleep(100);
                                            CoffeeIOMsg.Instance.AllBol = true;

                                            Thread.Sleep(5000);
                                            while (coffeeMsg.Instance.isMaking && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                            Thread.Sleep(1000);
                                            while ((!coffeeMsg.Instance.isMaking && !coffeeMsg.Instance.IsJBol) && (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                                            //  while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                            Thread.Sleep(2000);
                                            CoffeeIOMsg.Instance.AllBol = false;
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.KFWC_Robot_Left_Start);
                                            Thread.Sleep(100);
                                            while (true)
                                            {
                                                Thread.Sleep(200);
                                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.DLHD_Robot_Left_Sel);
                                                Thread.Sleep(200);
                                                if (CoffeeIOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }
                                            CoffeeMakeModel.Instance.IsDKFJL = true;
                                            Thread.Sleep(100);
                                            CoffeeIOMsg.Instance.AllBol = true;
                                            Thread.Sleep(100);
                                            CoffeeMakeModel.Instance.IsDLHDL = true;
                                            ZBBS = 1;
                                            while (!CoffeeMakeModel.Instance.IsLHWCL) { Thread.Sleep(10); }
                                            while (true)
                                            {
                                                RobotOperate.CX_Start_L();
                                                if (RobotOperate.pstatusL == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                {
                                                    break;
                                                }
                                                Thread.Sleep(100);
                                            }

                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.CCWC_Robot_Left_Sel);
                                                Thread.Sleep(100);
                                                if (CoffeeIOMsg.Instance.RobotBol)
                                                {
                                                    Console.WriteLine("出餐了");
                                                    break;
                                                }
                                            }
                                            Thread.Sleep(100);
                                            CoffeeIOSerialPortR.Instance.SerialSend(CoffeeIOModel.Instance.LHCC_Robot_Left_End);
                                            Thread.Sleep(100);
                                            CoffeeIOMsg.Instance.AllBol = true;
                                            CoffeeMakeModel.Instance.IsDCCL = true;
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                    break;
                                }
                                else
                                {
                                    CoffeeIOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.JBSB_Robot_Left_Start);
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.QB_Robot_Left_End);
                                    Thread.Sleep(200);
                                    CoffeeIOSerialPort.Instance.SerialSend(CoffeeIOModel.Instance.BJC_Robot_Left_End);
                                    Thread.Sleep(200);
                                    CoffeeIOMsg.Instance.AllBol = true;
                                    Thread.Sleep(200);
                                    QBMsg(MainWindow.cupCode);
                                    CoffeeIOMsg.Instance.AllBol = true;
                                    Thread.Sleep(500);
                                }
                                Thread.Sleep(10);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
            }
        }
        public byte[] GetUrlToBase(string url)
        {
#pragma warning disable CS0168 // 声明了变量，但从未使用过
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                //string Base64string = Convert.ToBase64String(Bytes);
                return Bytes;
            }
            catch (Exception e)
            {
                return new byte[0];
            }
#pragma warning restore CS0168 // 声明了变量，但从未使用过
        }
        public void IsClear()
        {
            try
            {
                CoffeeMakeModel.Instance.IsDDBDR = false;
                CoffeeMakeModel.Instance.IsDKFJL = false;
                CoffeeMakeModel.Instance.IsLHWCR = false;
                CoffeeMakeModel.Instance.IsLHWCL = false;
                CoffeeMakeModel.Instance.IsDCCL = false;
                CoffeeMakeModel.Instance.IsDBWCL = false;
                CoffeeMakeModel.Instance.IsDJBDL = false;
                CoffeeMakeModel.Instance.IsDJBJCL = false;
                CoffeeMakeModel.Instance.IsDJBL = false;
                CoffeeMakeModel.Instance.IsDLHDL = false;
                CoffeeMakeModel.Instance.IsDLHDR = false;
            }
            catch (Exception)
            {
            }
        }
    }
}
